import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Test extends JFrame implements ActionListener {
    private JComboBox<String> comboBox_name;
    private JRadioButton addBtn, subBtn, mulBtn, divBtn;
    private Button eql;
    private TextField a,b,ans;
    @Override
    public void actionPerformed(ActionEvent e) {
        try {
            // 获取输入数值和运算符
            double num1 = Double.parseDouble(a.getText());
            double num2 = Double.parseDouble(b.getText());
            String operator = "";
            if (addBtn.isSelected()) {
                operator = "+";
            } else if (subBtn.isSelected()) {
                operator = "-";
            } else if (mulBtn.isSelected()) {
                operator = "*";
            } else if (divBtn.isSelected()) {
                operator = "/";
            }
            // 计算结果并显示
            double result = 0;
            switch (operator) {
                case "+":
                    result = num1 + num2;
                    break;
                case "-":
                    result = num1 - num2;
                    break;
                case "*":
                    result = num1 * num2;
                    break;
                case "/":
                    if (num2 == 0) {
                        throw new ArithmeticException("Divide by zero");
                    }
                    result = num1 / num2;
                    break;
            }
            this.ans.setText(Double.toString(result));
        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(this, "Invalid input", "Error", JOptionPane.ERROR_MESSAGE);
        } catch (ArithmeticException ex) {
            JOptionPane.showMessageDialog(this, "Divide by zero", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
    public Test(){
        super("整形运算");
        //关闭窗口
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //表格长宽
        this.setSize(500,200);
        //表格的位置
        this.setLocationRelativeTo(null);
        //表格框架
        this.setLayout(new FlowLayout());
        //第一个文本行
        this.add(this.a = new TextField("0",10));
        //按钮
        // 创建单选按钮显示并选择运算符
        ButtonGroup operatorGroup = new ButtonGroup();
        operatorGroup.add(addBtn = new JRadioButton("+"));
        operatorGroup.add(subBtn = new JRadioButton("-"));
        operatorGroup.add(mulBtn = new JRadioButton("*"));
        operatorGroup.add(divBtn = new JRadioButton("/"));
        // 添加单选按钮到面板
        this.add(addBtn);
        this.add(subBtn);
        this.add(mulBtn);
        this.add(divBtn);
        //第二个文本行
        this.add(this.b = new TextField("0",10));
        //"="按钮
        this.eql = new Button("=");
        this.add(this.eql);
        this.eql.addActionListener(this);
        this.add(this.ans = new TextField(10));
        //可视化
        this.setVisible(true);
    }
    public static void main(String[] args) {
        new Test();
    }
}
